// StreetFire.js for PSPTube
// Made By JK108

function StreetFire_CheckURL( url, option )
{
	if (url.match("http://videos.streetfire.net") == "http://videos.streetfire.net"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a StreetFire URL
*/
function StreetFire_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/s2.addVariable\(\'video\',\'(.*?)\'/);
	var video_url = "http://px7.streetfire.net/streetfireFLV/" + video_url[1] + "-.flv";
	return video_url;
}

function StreetFire_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 20;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://videos.streetfire.net/videos/search.aspx?p=" + num + "&def=0&v=0&w=1&d=1&c=&t=" + keyword.replace(/ /g, "%20") + "&o=5&a=0" );

	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("<strong>0</strong> matches found") == "<strong>0</strong> matches found"){ result.total = 0; return result; }
	//var total = contents.match(/class\=\"membersListingMatches\"\>\<strong\>(.*?)\<\/strong\>/);
	//var total = total[1].replace(/,/g, "");
	result.total = 10000;

	var pos = contents.indexOf("<div class=\"videosListingPagRow\">");
	contents = contents.substring(pos + 33, contents.length);


	while(1){
	pos = contents.indexOf("<div class=\"videosListingPagRow\">");

	if(pos != -1){
		contents = contents.substring(pos + 33, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		pos = contents.indexOf("title=\"");
		var title = contents.substring(pos + 7, contents.length);
		pos = title.indexOf("\"");
		title = ImproveText(title.substring(0, pos).replace(/\n/g, " "));
		//Length
		var length = contents.match(/\<strong\>Duration\:\<\/strong\> (.*?)\<br\>/);
		var length = length[1].replace(/ /g, ":").replace(/m/g, "").replace(/s/g, "");
		if(length.substring(length.length - 2, length.length) == ":0"){length = length + "0";}
		var length = TimestringToSeconds(length);
		//Description
		pos = contents.indexOf("<strong>Description:</strong> ");
		var description = contents.substring(pos + 30, contents.length);
		pos = description.indexOf("<a href=");
		description = splitTo2Lines(ImproveText(description.substring(0, pos)), 60);
		//Views
		var views = contents.match(/\<strong\>Views\:\<\/strong\> (.*?)\<br\>/);
		var views = parseInt(views[1].replace(/,/g, ""));
		//Comments
		var comments = contents.match(/\/event\/VideoResults\/VideoComments\'\);\"\>(.*?)\<\/a\>/);
		var comments = parseInt(comments[1].replace(/,/g, ""));
		//Favourited
		var favourited = contents.match(/\<strong\>Favorited\:\<\/strong\> (.*?)\<br\>/);
		var favourited = parseInt(favourited[1].replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.CommentCount = comments;
		info.MylistCount = favourited;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var StreetFire = new Object();
StreetFire.Name          = "StreetFire";		/* Service name */
StreetFire.Description   = "StreetFire";		/* Service description */
StreetFire.SearchDesc    = "StreetFire";		/* Service search screen description */
StreetFire.SearchOSKMode = 1;				/* OSK mode 1: English only  2: Japanese */
StreetFire.CheckURL      = StreetFire_CheckURL;		/* CheckURL function */
StreetFire.GetURL        = StreetFire_GetURL;		/* GetURL function */
StreetFire.Search        = StreetFire_Search;		/* Search function */

// add StreetFire to site list
SiteList.push( StreetFire );
